<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-suffix=":"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备ID" prop="deviceId">
            <el-input v-model="form.deviceId" placeholder="请输入设备ID" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备类型" prop="deviceType">
            <el-select v-model="form.deviceType" placeholder="请选择设备类型" style="width: 100%">
              <el-option label="GPS设备" value="gps" />
              <el-option label="摄像头" value="camera" />
              <el-option label="传感器" value="sensor" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车队ID" prop="fleetId">
            <el-input v-model.number="form.fleetId" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
              <el-option label="已绑定" value="bound" />
              <el-option label="未绑定" value="unbound" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="loading">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';

defineOptions({
  name: 'DeviceForm'
});

const dialogVisible = ref(false);
const title = ref('');
const loading = ref(false);
const formRef = ref<any>();
const fleetId = ref<number>(0);

const form = reactive({
  id: null,
  deviceId: '',
  deviceType: '',
  fleetId: 0,
  status: 'bound'
});

const rules = reactive({
  deviceId: [
    { required: true, message: '请输入设备ID', trigger: 'blur' }
  ],
  deviceType: [
    { required: true, message: '请选择设备类型', trigger: 'change' }
  ],
  fleetId: [
    { required: true, message: '车队ID不能为空', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
});

// 接收父组件参数
const acceptParams = (params: any) => {
  title.value = params.title;
  fleetId.value = params.fleetId;
  form.fleetId = params.fleetId;
  
  if (params.row && Object.keys(params.row).length > 0) {
    Object.assign(form, params.row);
  } else {
    // Reset form for new entry
    Object.assign(form, {
      id: null,
      deviceId: '',
      deviceType: '',
      fleetId: params.fleetId,
      status: 'bound'
    });
  }
  
  dialogVisible.value = true;
};

// 关闭弹窗
const handleClose = () => {
  dialogVisible.value = false;
  formRef.value?.resetFields();
};

// 提交数据
const handleSubmit = () => {
  if (!formRef.value) return;
  
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) return;
    
    loading.value = true;
    try {
      // 这里应该调用实际的API
      if (form.id) {
        // 编辑操作
        console.log('编辑设备绑定:', form);
      } else {
        // 新增操作
        console.log('新增设备绑定:', form);
      }
      
      ElMessage.success(`${title.value}成功！`);
      handleClose();
      // 刷新列表
      // params.getTableList && params.getTableList();
    } catch (error) {
      console.error(`${title.value}失败:`, error);
      ElMessage.error(`${title.value}失败！`);
    } finally {
      loading.value = false;
    }
  });
};

defineExpose({
  acceptParams
});
</script>

<style scoped lang="scss">
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 20px 20px;
  box-sizing: border-box;
}
</style>